<template>
    <div class="user b">
        <!-- 主页图标 -->
        <div class="home-logo" @click="toIndex">
          <img src="../assets/image/Home.png" alt="" class="img-home" >
          <span class="text-home">HOME</span>
        </div>
        <!-- 登录与后台 -->
        <div class="link">
          <img src="../assets/image/Login.png" alt="" class="img-login"  @click="toLogin">
          <img src="../assets/image/Setting.png" alt="" class="img-setting" @click="toSetting">
        </div>
        <!-- 登录框 -->
        <div class="block bk1">
            <div class="login-text">登录</div>
            <div class="div-login-input b">
                <input placeholder="请输入用户名" class="login-input" v-model="Uname">
            </div>
            <div class="div-login-input b">
                <input @keyup.enter="login()" placeholder="请输入密码" class="login-input" v-model="Upass">
            </div>
        </div>
        <!-- 注册框 -->
        <div class="block bk2">
            <div class="login-text">注册</div>
            <div class="div-login-input b">
                <input placeholder="请输入用户名" class="login-input" v-model="Rname">
            </div>
            <div class="div-login-input b">
                <input placeholder="输入密码" class="login-input" v-model="Rpass1">
            </div>
            <div class="div-login-input b">
                <input @keyup.enter="register()" placeholder="确认密码" class="login-input" v-model="Rpass2">
            </div>
        </div>
        <div class="line l0 b"></div>
        <div class="line l1 b"></div>
        <div class="line l2 "></div>
        <div class="line l3 b"></div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
    data:function(){
        return{
            Uname:'',
            Upass:'',
            Rname:'',
            Rpass1:'',
            Rpass2:''
        }
    },
    computed:{
        ...mapState(['user'])
    },
    methods:{
        // 登录
        login:function(){
            this.$axios.get('/api/checkUserByUname', {
                params:{
                    uname:this.Uname,
                    upass:this.Upass
                }
            }).then((response)=>{
                this.$set(this.user,"uname", response.data)
                console.log(response.data);
                console.log(this.user);
                if (this.user.uname != null) {
                    this.$notify({
                        title: 'SUCCESS',
                        message: '登录成功'
                    });
                    setTimeout(()=>{
                        this.$router.push('/index')
                    },2000)
                } else {
                    this.$notify({
                        title: 'FAIL',
                        message: '登录失败'
                    });
                }
            })
        },
        // 注册
        register:function(){
            if (this.Rpass1 === this.Rpass2 && this.Rname !== "") {
                this.$axios({
                    url:'/api/insertUser',
                    method:'post',
                    data:[
                        JSON.stringify({
                            uid:'',
                            uname:this.Rname,
                            userPassword:this.Rpass1,
                            userFavouriteList:''
                        })
                    ]
                }, {headers: {'Content-Type':'application/json; charset=utf-8'}}).then((response)=>{
                    console.log(response.data);
                    if (response.data != null) {
                            this.$notify({
                                title: '√',
                                message: '注册成功'
                            });
                    }
                })
            }
        },
        // 转去主页
        toIndex:function() {
            this.$router.push('/index')
        },
        // 转去后台
        toSetting:function(){
            this.$router.push('/setting')
        },
        // 转去登录
        toLogin:function(){
            this.$router.push('/login')
        },
    }
}
</script>

<style scoped>
.line{
    height: 400px;
    width: 0;
    border-left: none;
    transform: skewX(-45deg);
}
.l1{
    position: absolute;
    bottom: 0;
    right: 0;
}
.l2{
    position: absolute;
    left: 45%;
    top: 22%;
    height: 770px;
    border: solid 1px black;
}
.l3{
    position: relative;
    bottom: 0;
    right: 0;
}
.block{
    width: 400px;
    height: 500px;
    padding: 10px;
}
.bk1{
    position: absolute;
    left: 25%;
    top: 200px;
}
.bk2{
    position: absolute;
    left: 55%;
    top: 400px;
}
.login-text{
    font-size: 30px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    font-family: "黑体";
    padding-bottom: 10px;
}
.login-input{
    width: 95%;
    height: 38px;
    font-size: 19px;
    outline: none;
    border: none;
}
.div-login-input{
    margin-top: 30px;
    width: 100%;
    height: 40px;
    overflow: hidden;
    padding-left: 10px;
}
.b{
  border: 2px black solid;
}
.user{
  width: 100%;
  min-height:98vh;
  position: relative;
  overflow: hidden;
}
.home-logo{
  position:absolute;
  left: 20px;
  top: 15px;
  width: 5%;
  height: 40px;
  cursor: pointer;
}
.img-home{
  position:relative;
  left: 0;
  width: 35px;
}
.text-home{
  position:absolute;
  left: 45px;
  top: 10px;
  font-size: 15px;
}
.link{
  position:absolute;
  left: 1750px;
  top: 20px;
  width: 5.5%;
  height: 50px;
}
.img-login{
  position:absolute;
  left: 0;
  width: 50px;
  cursor: pointer;
}
.img-setting{
  position:absolute;
  right: 0;
  top: 5px;
  width: 40px;
  cursor: pointer;
}
</style>